$(function () {
    //全局定义页数与总条数
    var markid = getUrlString('markid');
    var type = getUrlString('type');
    var key = getUrlString('key');
    var userid = getUrlString('userid');
    var fenlei = getUrlString('fenlei');
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    var page = -1,totalSize = 0,sort= 0;


    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        isLockX:false,
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#pullEggList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                getDate(page)
            }
        }
    });

    //发表社区
    $('#releasePullEgg img').click(function () {
        // window.location.href = 'dancool://postweibo=';
        window.location.href = 'dancool://fullweb='+urlView+'pullEgg/releasePullEgg.html?type=1';
    });

    /*联网加载列表数据*/
    function getDate(page) {
        //延时一秒,模拟联网
        var params = {
            'key':key,
            'userid':userid,
            'fenlei':fenlei,
            'type':type,
            'markid':markid,
            'page': page,
        };
        getData('get', '/niu/apiv3/weibo_chatlistv2.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let append = '';
                $(data.data).each(function () {
                    var chenghaoStr = (this.userlv).substring(3);

                    append+='<li>' +
                        '           <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                    <div class="communityUser">' +
                        '                       <input type="hidden" class="userId" value="'+this.userid+'">'+
                        '                        <div class="communityUserPic">' +
                        '                            <img src="'+this.userface+'" alt="">' +
                        '                        </div>' +
                        '                        <div class="communityUserName">' +
                        '                            <p>' +
                        '                                <span>'+this.usernick+'</span>' ;
                        if(this.uservip === '1'){
                            append +=  '               <img style="width: 1rem" src="../../res/img/eggTV/monthCard@2x.png" alt="">' ;
                        }
                        if (chenghaoStr < 6 && chenghaoStr > 0){
                            append += '<img src="../../res/img/common/bronzeIcon.png" alt="">' ;
                        }else if (5 < chenghaoStr && chenghaoStr < 11){
                            append += '<img src="../../res/img/common/silverIcon.png" alt="">' ;
                        }else if (10 < chenghaoStr && chenghaoStr < 16){
                            append += '<img src="../../res/img/common/goldIcon.png" alt="">' ;
                        }else if (15 < chenghaoStr && chenghaoStr < 21){
                            append += '<img src="../../res/img/common/platinaIcon.png" alt="">' ;
                        }else if(20 < chenghaoStr && chenghaoStr < 26){
                            append += '<img src="../../res/img/common/diamondIcon.png" alt="">' ;
                        }
                    if(this.chenghao !== ''){
                        append += '<img style="height: 1rem;width: initial" src="https://api.odancool.com'+this.huiyuaninfo2+'" alt="">                  </p>' ;
                    }
                       append+= '                            <p>'+subTime(this.time)+'</p>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                    <div class="title1">'+this.intro+'</div>' ;

                        if(this.picurls !== ''){
                            var picUrls =  this.picurls;
                            var picUrlsArr = picUrls.split(';');

                            append +=  '<div class="infoImg">' ;
                            for (var i = 0;i < picUrlsArr.length;i++){
                                if (picUrlsArr.length === 1){
                                    append += '<img style="width: initial;max-height: 256px;" src="'+picUrlsArr[i]+'-narrow200" alt="">' ;
                                }else if (picUrlsArr.length === 2 || picUrlsArr.length === 4){
                                    append += '<img style="width: 47%" src="'+picUrlsArr[i]+'-toysbox22" alt="">' ;
                                }else{
                                    append += '<img src="'+picUrlsArr[i]+'-toysbox22" alt="">' ;
                                }
                            }

                            append += '</div>' ;
                        }

                        append += '          <div class="operation">' +
                            '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                        <div class="comment">' +
                        '                            <img src="../../res/img/pullEgg/pl.png" alt="">' +
                        '                            <span>'+this.plcount+'</span>' +
                        '                        </div>' +
                        '                        <div class="like">' +
                        '                            <input type="hidden" class="postid" value="'+this.id+'"> '+
                        '                            <img src="../../res/img/pullEgg/dz.png" alt="">' +
                        '                            <span class="zancount">'+this.zancount+'</span>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                </li>'
                });
                $('#pullEggList').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }
                totalSize = totalSize + 10;
                if (totalSize > data.datacount)
                    mRefresh.endUpLoading(true);
                else{
                    mRefresh.endUpLoading(false);
                }

                //打开用户库
                $('#pullEggList li .communityUser').click(function () {
                    var userId = $(this).find('.userId').val();
                    window.location.href = 'dancool://member='+userId;
                });

                //点赞
                $('#pullEggList li .operation .like').click(function () {
                    var postid = $(this).find('.postid').val();
                    var zancount = $(this).find('.zancount').html();
                    var _this = $(this).find('.zancount');
                    var params = {
                        'markid':markid,
                        'weiboid':postid
                    };
                    getData('get', '/niu/apiv3/weibo_dolike.php', params, function (data) {
                        console.log(data);
                        if(data.resultcode === '0'){
                            if(data.command === 'add'){
                                _this.html(numAdd(zancount,1))
                            }else {
                                _this.html(numSub(zancount,1))
                            }
                            laymsg(data.msg)
                        }else {
                            laymsg(data.msg)
                        }
                    })
                });

                //去详情
                $('#pullEggList li .title1,#pullEggList li .operation .comment,#pullEggList li .infoImg').click(function () {
                    var id = $(this).prevAll('.id').val();
                    window.location.href = 'dancool://web='+urlView+'pullEgg/pullEggDetail.html?id='+id;
                    // window.location.href = 'dancool://weibo='+id;
                });

            }else {
                laymsg(data.msg)
            }

        });
    }

});
